﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;

namespace RoboSensors.Tools
{
    public class WpfImageConvert
    {
        static public BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
        {
            using (Bitmap bitmapTep = new Bitmap(bitmap))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    bitmapTep.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    byte[] bytes = ms.GetBuffer();  //byte[]   bytes=   ms.ToArray(); 这两句都可以
                    ms.Close();
                    //Convert it to BitmapImage
                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.StreamSource = new MemoryStream(bytes);
                    image.EndInit();
                    return image;
                }
            }
        }

        /// <summary>
        /// WriteableBitmap转Bitmap
        /// </summary>
        /// <param name="writeBmp"></param>
        /// <returns></returns>
        static public System.Drawing.Bitmap WriteableBitmap2Bitmap(WriteableBitmap writeBmp)
        {
            System.Drawing.Bitmap bmp;
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create((BitmapSource)writeBmp));
                enc.Save(outStream);
                bmp = new System.Drawing.Bitmap(outStream);
            }
            return bmp;
        }

        /// <summary>
        /// 通过构造函数方法将BitmapImage转WriteableBitmap
        /// 因为BitmapImage的基类是BitmapSource
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        static public WriteableBitmap BitmapImage2WriteableBitmap(BitmapImage image)
        {
            return new WriteableBitmap(image);
        }

        static public unsafe void Bitmap2WriteableBitmap(Bitmap image, ref WriteableBitmap writeableBitmap)
        {
            //using (Bitmap bitmapTmp = new Bitmap(image))
            //{
            using (MemoryStream ms = new MemoryStream())
            {
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);//bmp格式第55位开始才是有效数据，即[54]开始
                byte[] bytes = ms.GetBuffer();
                byte[] data = new byte[image.Width * image.Height * 4];
                int size = image.Width * image.Height;
                int range = size + 54;
                //如果把range拆开再for循环里写，则会慢非常多
                //因为每一次for循环都重新算一遍，for循环里尽量不要计算
                for (int i = 54, k = 0; i < range; i++, k = k + 4)
                {
                    data[k] = bytes[i];
                    data[k + 1] = bytes[i + 1];
                    data[k + 2] = bytes[i + 2];
                    data[k + 3] = bytes[i + 3];
                }
                writeableBitmap.WritePixels(new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight), data, writeableBitmap.PixelWidth * 4, 0);
            }
            // }
            // return colorBitmap;
        }
    }
}
